package com.fjec.paid.service.impl;

import com.fjec.common.util.DateUtil;
import com.fjec.common.ys.PaidHelper;
import com.fjec.paid.service.IBizOrderPaymentService;
import com.fjec.paid.service.YsPayDelayQueryService;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;

@Service
public class YsPayDelayQueryServiceImpl implements YsPayDelayQueryService {
    @Autowired
    private PaidHelper paidHelper;

    @Resource
    private IBizOrderPaymentService bizOrderPaymentService;

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void handle(String orderId) throws Exception {
        Map<String, Object> payment = bizOrderPaymentService.getPaymentByOrderId(Long.valueOf(orderId));
        if (payment == null || MapUtils.isEmpty(payment))
            return;

        String outTradeNo = MapUtils.getString(payment, "outTradeNo");
        String tradeNo = MapUtils.getString(payment, "tradeNo");
        String shopDate = MapUtils.getString(payment, "shopDate");
        Integer paymentType = MapUtils.getInteger(payment, "paymentType");

        Map<String, Object> res = paidHelper.queryPayment(outTradeNo, tradeNo, shopDate);

        String tradeStatus = MapUtils.getString(res, "trade_status");
        String now = DateUtil.formatNow("yyyy-MM-dd HH:mm:ss");
        String totalAmount = MapUtils.getString(res, "total_amount");
//        String accountDate = MapUtils.getString(res, "account_date");
        bizOrderPaymentService.payNotify(now, outTradeNo, totalAmount, tradeNo, tradeStatus, now, paymentType);
    }
}
